<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/17 0017
 * Time: 上午 9:53
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Cate extends BasicAdmin
{
    public $table = 'Cate';
    public $lists = 'CateList';

    public function index(){
        $pid = $this->request->get('id');
        $title = Db::name('CategoryModule')->where('id',$pid)->value('name');
        $this->title = $title;
        $db = Db::name($this->table)->where(['pid'=>$pid,'is_deleted'=>1])->order('sort asc,id asc');
        return parent::_list($db,false);
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $id = $this->request->post('id');
        $id_arr = explode(',', $id);
        foreach ($id_arr as $i) {
            if (in_array($i, [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50])) {
                $this->error('请勿删除默认模块');
            }
        }
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 内容分为content和lists类型，不同模版
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function content(){
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['content'] = json_encode($post['content'],320);
            DataService::save($this->table, $post);
            $this->success('保存成功！', '');
        }
        $id = $this->request->get('id');
        $vo = Db::name($this->table)->where('id',$id)->find();

        $pid = $this->request->get('pid') ?: $vo['pid'];
        $title = Db::name('CategoryModule')->where('id',$pid)->value('name');
        $vo['content'] = json_decode($vo['content'],true);

        $this->assign([
            'pid'=>$pid,
            'title'=>$title . ' - ' . '添加/编辑',
            'vo'=>$vo
        ]);
        return view(isset($vo['template']) ? $vo['template'] : '');
    }

    public function lists(){
        $id = $this->request->get('id');
        $vo = Db::name($this->table)->where('id',$id)->find();
        $title = Db::name('CategoryModule')->where('id',$vo['pid'])->value('name');
        $this->title = $title . ' - ' . $vo['title'];

        $db = Db::name($this->lists)->where(['is_deleted'=>1,'pid'=>$id])->order('sort asc,id asc');
        return parent::_list($db,true,true,false,[],$vo['template']);
    }

    public function _lists_data_filter(&$data){
        foreach ($data as &$v){
            $v['content'] = json_decode($v['content'], true);
        }
    }

    public function lists_add(){
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['content'] = json_encode($post['content'],320);
            DataService::save($this->lists, $post);
            $this->success('保存成功！', '');
        }
        $id = $this->request->get('id');
        $vo = Db::name($this->lists)->where('id',$id)->find();

        $pid = $this->request->get('pid') ?: $vo['pid'];
        $cate = Db::name($this->table)->field('title,template')->where('id',$pid)->find();
        $vo['content'] = json_decode($vo['content'],true);

        $this->assign([
            'pid'=>$pid,
            'title'=>$cate['title'] . ' - ' . '添加/编辑',
            'vo'=>$vo
        ]);
        return view($cate['template'].'_add');
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_forbid()
    {
        if (DataService::update($this->lists)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_resume()
    {
        if (DataService::update($this->lists)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function lists_del()
    {
        if (DataService::update($this->lists)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}